<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of PaymentForm
 *
 * @author Sorasak Siangsanan
 */
class PaymentForm extends CFormModel {

    public $order;
    public $bank;
    public $bankBranch;
    public $payment_name;
    public $payment_mobilephone;
    public $amount;
    public $payment_date;
    public $time;
    public $remark;

    public function rules() {
        return array(
            array('payment_date', 'checkRequired'),
            array('bank, payment_name, payment_mobilephone, payment_date, amount, time', 'required'),
            array('amount', 'match', 'pattern' => '/^[0-9]{1,12}(\.[0-9]{0,4})?$/'),
        );
    }

    public function checkRequired($attribute, $params) {
        if (empty($this[$attribute])) {
            $this->addError($attribute, Yii::t('payment', $attribute) . ' ' . Yii::t('mywish', 'required'));
        }
    }

    public function getPaymentOrder() {
        $order = Order::model()->find(array('condition' => 'cust_id=' . Yii::app()->user->id . ' AND status_id in (1)'));
        return $order;
    }

    public function attributeLabels() {
        return array(
            'order' => Yii::t('order_model', 'order_id'),
            'bank' => Yii::t('payment', 'bank'),
            'bankBranch' => Yii::t('payment', 'bank_branch'),
            'payment_name' => Yii::t('payment', 'full_name'),
            'payment_mobilephone' => Yii::t('order_model', 'contact_tel'),
            'amount' => Yii::t('payment', 'amount'),
            'package' => Yii::t('payment', 'package'),
            'price' => Yii::t('payment', 'price'),
            'time' => Yii::t('payment', 'time'),
            'payment_date' => Yii::t('payment', 'payment_date'),
            'remark' => Yii::t('payment', 'remark'),
        );
    }

    public function payment() {
        $payment = new Payment();
        $payment->order_id = $this->order;
        $payment->bank = $this->bank;
        $payment->bank_branch = $this->bankBranch;
        $payment->payment_date = ConvertFormatUtil::BEStringToADString($this->payment_date, false);
        $payment->time = $this->time;
        $payment->full_name = $this->payment_name;
        $payment->mobilephone = $this->payment_mobilephone;
        $payment->amount = $this->amount;
        $payment->remark = $this->remark;
        $payment->create_date = date("Y-m-d H:i:s");
        if ($payment->save()) {
            $order = Order::model()->findByPk($this->order);
            $order->status_id = 2;
            if ($order->update()) {
                return true;
            }
            return false;
        }
        return false;
    }

    public function getGender($gender) {
        switch ($gender) {
            case 1:
                return Yii::t('register', 'male');
            case 2:
                return Yii::t('register', 'female');
            default:
                break;
        }
    }

    public function getPackageName($packageId) {
        $package = Package::model()->findByPk($packageId);
        return $package["package_name_" . Yii::app()->language];
    }

}
